package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.fastdfs.FastDFSClientUtil;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.model.wemedia.pojos.WmNewsMaterial;
import com.heima.utils.common.WmUserThreadLocal;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.mapper.WmNewsMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private FastDFSClientUtil fastDFSClientUtil;

    @Value("${fdfs.url}")
    private String fdfsUrl;

    /**
     * 图片保存
     *
     * @param multipartFile
     * @return
     */
    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) {

        //判断参数是否为空
        if (null == multipartFile) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        try {
            //将图片存入fastDfs中
            String file = fastDFSClientUtil.uploadFile(multipartFile);

            //判断是否保存成功
            if (null == file) {
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_IMAGE_FORMAT_ERROR);
            }

            //封装对对象
            WmMaterial wmMaterial = new WmMaterial();
            wmMaterial.setUrl(fdfsUrl + file);
            wmMaterial.setUserId(WmUserThreadLocal.getUserId());
            wmMaterial.setType((short) 0);
            wmMaterial.setIsCollection((short) 0);
            wmMaterial.setCreatedTime(new Date());

            //保存
            boolean save = save(wmMaterial);

            //响应
            if (save) {
                return ResponseResult.okResult(wmMaterial);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    /**
     * 素材列表
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findList(WmMaterialDto dto) {
        //判断参数是否为空
        if (null == dto) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        dto.checkParam();

        //构建分页对象
        Page<WmMaterial> materialPage = new Page<>(dto.getPage(), dto.getSize());

        //构建查询对象
        LambdaQueryWrapper<WmMaterial> wrapper = Wrappers.lambdaQuery();

        //构建当前用户
        wrapper.eq(WmMaterial::getUserId, WmUserThreadLocal.getUserId());

        //判断是否根据收藏查询
        if (dto.getIsCollection() == 1) {
            wrapper.eq(WmMaterial::getIsCollection, dto.getIsCollection());
        }

        //查询数据
        IPage<WmMaterial> iPage = page(materialPage, wrapper);

        //封装对象
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) iPage.getTotal());
        result.setData(iPage.getRecords());
        return result;
    }

    @Autowired
    private WmNewsMaterialMapper wmNewsMaterialMapper;

    /**
     * 删除图片
     * @param id
     * @return
     */
    @Override
    public ResponseResult delPicture(Integer id) {

        //判断参数是否为空
        if (null == id) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //查询媒体引用 是否有文章引用
        LambdaQueryWrapper<WmNewsMaterial> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(WmNewsMaterial::getMaterialId, id);
        Integer integer = wmNewsMaterialMapper.selectCount(wrapper);

        //如果查到 返回无法删除
        if (integer > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH,"资源被引用无法删除");
        }

        //如果未查到 删除资源
        boolean byId = removeById(id);

        //判断是否删除成功
        if (byId) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS);
        }

        return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
    }

    /**
     * 收藏与取消收藏
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult collectionMaterial(Integer id, Short isCollection) {

        //判断参数是否为空
        if (null == id || null == isCollection) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //创建修改对象
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setId(id);
        wmMaterial.setIsCollection(isCollection);

        //修改收藏数据
        boolean update = updateById(wmMaterial);

        //判断是否修改成功
        if (update) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS);
        }

        return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
    }
}
